package com.jzg.sys.manager;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzg.sys.common.util.CommonUtils;
import com.jzg.sys.dao.entity.SecMenu;
import com.jzg.sys.dao.entity.SecRole;
import com.jzg.sys.dao.entity.SecUser;
import com.jzg.sys.dao.entity.SecUserRole;
import com.jzg.sys.dao.mapper.SecMenuMapper;
import com.jzg.sys.dao.mapper.SecRoleMapper;
import com.jzg.sys.dao.mapper.SecUserMapper;
import com.jzg.sys.dao.mapper.SecUserRoleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
    * 用户:角色 服务实现类
    * </p>
 *
 * @author lzp
 * @since 2019-02-22
 */
@Service
public class SecUserRoleManager extends ServiceImpl<SecUserRoleMapper, SecUserRole>  {
    @Resource
    private SecUserRoleMapper userRoleMapper;
    @Resource
    private SecUserMapper secUserMapper;
    @Resource
    private SecMenuMapper secMenuMapper;

    /**
     * 根据角色id获取对应的用户
     * @param page
     * @param roleId
     * @return
     */
    public IPage<SecUser> findNewUserPageWithRoleId(Page<SecUser> page, String roleId,SecUser user) {
        if(StringUtils.isNotBlank(user.getUserCode())){
            user.setUserCode("%"+user.getUserCode()+"%");
        }
        if(StringUtils.isNotBlank(user.getUserName())){
            user.setUserName("%"+user.getUserName()+"%");
        }
        page.setTotal(secUserMapper.countNewUserPageWithRoleId(roleId,user).longValue());
        page.setRecords(secUserMapper.findNewUserPageWithRoleId(roleId,user,page));
        return page;
    }

    /**
     * 删除角色用户
     * @param roleId
     * @param userId
     */
    public void deleteUser(String roleId, String userId) {
        userRoleMapper.deleteUser(roleId,userId);
    }

    /**
     * 获取没有分配角色的用户
     * @param page
     * @param roleId
     * @param user
     * @return
     */
    public IPage<SecUser> findUserPageExceptRoleId(Page<SecUser> page,String roleId,SecUser user) {
        if(StringUtils.isNotBlank(user.getUserCode())){
            user.setUserCode("%"+user.getUserCode()+"%");
        }
        if(StringUtils.isNotBlank(user.getUserName())){
            user.setUserName("%"+user.getUserName()+"%");
        }
        page.setTotal(secUserMapper.countUserPageExceptRoleId(roleId,user).longValue());
        page.setRecords(secUserMapper.findUserPageExceptRoleId(roleId,user,page));
        return page;
    }

    /**
     * 保存角色用户
     * @param roleId
     * @param secUsers
     */
    public  void saveRoleUser(String roleId, JSONArray secUsers){
        for (int i = 0; i < secUsers.size(); i++) {
            SecUser user = secUsers.getObject(i, SecUser.class);
            SecUserRole ur = new SecUserRole();
            ur.setUserRoleId(CommonUtils.getUUID());
            ur.setUserId(user.getUserId());
            ur.setRoleId(roleId);
            userRoleMapper.insert(ur);
        }
    }
}
